# PlanetfallSpellsEntityConvers.py
# Planetfall Spells

# When an entity (unit, building, etc) needs to be selected as a target, and
# the mission started, we can't go around just using the entity class, since
# some entities don't have classes (buildings, for example, are on a plot
# and need a building id). So, we'll pass them around using target classes.

from CvPythonExtensions import *
gc = CyGlobalContext()

#
# Base target class
#
class Target:
    def __init__(self):
        self.iData1 = -1
        self.iData2 = -1
        self.iData3 = -1

    def getData(self):
        return (self.iData1, self.iData2, self.iData3)

    
#######
# Plot
#######

# Factory Methods
def getPlotTargetFromPlot(pPlot):
    pPlotTarget = PlotTarget()
    pPlotTarget.iData1 = pPlot.getX()
    pPlotTarget.iData2 = pPlot.getY()
    pPlotTarget.iData3 = -1
    return pPlotTarget

def getPlotTargetFromData(pTuple):
    pPlotTarget = PlotTarget()
    pPlotTarget.iData1 = pTuple[0]
    pPlotTarget.iData2 = pTuple[1]
    pPlotTarget.iData3 = pTuple[2]
    return pPlotTarget

# Class
class PlotTarget(Target):
    def __init__(self):
        Target.__init__(self)

    def getPlot(self):
        return CyMap().plot(self.iData1, self.iData2)


########
# Unit
########

# Factory Methods
def getUnitTargetFromUnit(pUnit):
    pUnitTarget = UnitTarget()
    pUnitTarget.iData1 = pUnit.getOwner()
    pUnitTarget.iData2 = pUnit.getID()
    return pUnitTarget

def getUnitTargetFromData(pTuple):
    pUnitTarget = UnitTarget()
    pUnitTarget.iData1 = pTuple[0]
    pUnitTarget.iData2 = pTuple[1]
    pUnitTarget.iData3 = pTuple[2]
    return pUnitTarget

# Class
class UnitTarget(Target):
    def __init__(self):
        Target.__init__(self)

    def getUnit(self):
        return gc.getPlayer(self.iData1).getUnit(self.iData2)
#############
# Buildings
#############

# Factory Methods
def getBuildingTargetFromPlotBuilding(pPlot, iBuilding):
    pPlotTarget = BuildingTarget()
    pPlotTarget.iData1 = pPlot.getX()
    pPlotTarget.iData2 = pPlot.getY()
    pPlotTarget.iData3 = iBuilding
    return pPlotTarget

def getBuildingTargetFromCityBuilding(pCity, iBuilding):
    pPlotTarget = BuildingTarget()
    pPlotTarget.iData1 = pCity.getX()
    pPlotTarget.iData2 = pCity.getY()
    pPlotTarget.iData3 = iBuilding
    return pPlotTarget

def getBuildingTargetFromData(pTuple):
    pPlotTarget = BuildingTarget()
    pPlotTarget.iData1 = pTuple[0]
    pPlotTarget.iData2 = pTuple[1]
    pPlotTarget.iData3 = pTuple[2]
    return pPlotTarget
    
# Class
class BuildingTarget(Target):
    """
    iData1: The city's X-coordinate
    iData2: The city's Y-coordinate
    iData3: The building type.
    """
    def __init__(self):
        Target.__init__(self)

    def getCity(self):
        return CyMap().plot(self.iData1, self.iData2).getPlotCity()

    def getBuilding(self):
        return self.iData3
